<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="宠物名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSale">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="offSale">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>


        <!--列表-->
        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="编号"  width="60">
            </el-table-column>
            <el-table-column prop="name" label="宠物名" >
            </el-table-column>
            <el-table-column prop="saleprice" label="销售价格" >
            </el-table-column>
            <el-table-column prop="costprice" label="成本价格" >
            </el-table-column>
            <el-table-column prop="state" label="上架状态" >
                <template scope="scope">
                    <span v-if="scope.row.state==1" style="color: green">上架</span>
                    <span v-if="scope.row.state==0" style="color: red">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" >
            </el-table-column>
            <el-table-column prop="type.name" label="宠物类型" >
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="total, sizes, prev, pager, next, jumper"
                           @current-change="handleCurrentChange"
                           @size-change="handleSizeChange"
                           :current-page="currentPage"
                           :page-size="pageSize"
                           :page-sizes="pageSizes"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

    </section>
</template>

<script>
    export default {
        data(){
            return{
                users:[],//双向绑定 列表数据
                listLoading: false, //加载框
                sels:[],  //列表选中项
                currentPage:1,//当前页
                pageSize:5,//默认显示每页条数
                pageSizes:[5,10,10], //每页条数
                total:0,//总条数
                filters:{
                    name:''
                },
            }
        },
        methods:{
            //当页码发生改变时 触发
            handleCurrentChange(val){
                this.currentPage = val;
                this.loadPet();
            },
            //当每页条数发生变化时 触发
            handleSizeChange(val){
                this.currentPage = 1;
                this.pageSize = val;
                this.loadPet();
            },
            selsChange(sels){
                this.sels = sels;
            },
            //批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/employee/batchDelEmployee",ids).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadPet();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //模糊查询 按钮
            getUsers(){
                this.loadPet();
            },
            //上架
            onSale(){
                if(!this.sels.length){
                    this.$message.error('请选中数据进行上架!!');
                    return;
                }
                //调用上架方法
                this.$http.patch("/pet/onSale", this.sels).then((result => {
                    if(result.data.success){
                        this.loadPet();
                    }
                }));
            },
            //下架
            offSale(){
                if(!this.sels.length){
                    this.$message.error('请选中数据进行下架!!');
                    return;
                }
                //调用下架方法
                this.$http.patch("/pet/offSale", this.sels).then((result => {
                    if(result.data.success){
                        this.loadPet();
                    }
                }));
            },
            //加载员工列表
            loadPet(){
                let partm = {
                    "currentPage":this.currentPage,
                    "pageSize":this.pageSize,
                    "username":this.filters.name
                };
                this.$http.patch("/pet/loadPet",partm).then((res => {
                    this.users = res.data.list;
                    this.total = res.data.total;
                    console.debug("加载员工列表",res);
                }));
            },
        },
        mounted(){
            this.loadPet();
        }
    }
</script>